<!-- 公司资料维护 -->
<template>
  <div class="cp-data-maintain">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>基本信息列表</span>
      </div>
      <div class="content">
        <el-row>
          <el-col :offset="4" :span="16">
            <el-form :model="formData" label-width="100px" size="medium" :rules="rules" ref="formData">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="公司全称" prop="companyName">
                    <el-input v-model="formData.companyName"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="公司简称">
                    <el-input v-model="formData.ZJM"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="联系人" prop="fullName">
                    <el-input v-model="formData.fullName"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="联系电话" prop="mobile">
                    <el-input v-model="formData.mobile"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="传真">
                    <el-input v-model="formData.fax"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="16">
                  <el-form-item label="公司地址" prop="address">
                    <el-input v-model="formData.address"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-col>
        </el-row>
        <div class="card-footer">
          <el-button type="warning" @click="sureSave('formData')">保存信息</el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { GetUserProfile, saveUserProfile } from './api/customerManagement'
export default {
  data () {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('联系电话不能为空'))
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入正确的手机号'))
        }
      }
    }
    return {
      rules: {
        companyName: [
          { required: true, message: '请输入公司全称', trigger: 'blur' }
        ],
        mobile: [
          { required: true, validator: checkPhone, trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入公司地址', trigger: 'blur' }
        ],
        fullName: [
          { required: true, message: '请输入联系人', trigger: 'blur' }
        ]
      },
      formData: {
        companyName: '',
        ZJM: '',
        fullName: '',
        mobile: '',
        fax: '',
        address: ''
      }
    }
  },
  mounted () {
    this.getUserProfile()
  },
  methods: {
    // 获取公司维护资料
    async getUserProfile () {
      const res = await GetUserProfile()
      if (res.status === 200) {
        Object.assign(this.formData, res.data[0])
        console.log(this.formData, '55555555555')
      } else {
        this.$notify({
          title: '错误',
          message: res.msg,
          type: 'error'
        })
      }
    },
    // 修改公司维护资料
    async saveNews () {
      const res = await saveUserProfile({
        companyName: this.formData.companyName,
        ZJM: this.formData.ZJM,
        fullName: this.formData.fullName,
        mobile: this.formData.mobile,
        fax: this.formData.fax,
        address: this.formData.address
      })
      if (res.status === 200) {
        this.$notify({
          title: '提示',
          message: res.msg,
          type: 'success'
        })
      } else {
        this.$notify({
          title: '错误',
          message: res.msg,
          type: 'error'
        })
      }
    },
    // 确认修改
    sureSave (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.saveNews()
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="less">
  .card-footer{
    margin-top: 20px;
    padding-top: 20px;
    text-align: center;
    border-top: 1px solid #ebeef5;
  }
</style>
